'use strict';

$(function () {

    var trigger_img = null;
    var show_img = null;

    $(document).on('click', 'tr.items img,tr.item img', function (e) {
        var target = $(this);
        var img = target.attr('src');
        var obj = $($('.layer_model').html());
        obj.find("img").attr('src', img);
        obj.find("input.txt-link").attr('value', this.getAttribute('link'));
        obj.find("input.txt-des").attr('value',this.getAttribute('des'));
        obj.find("input.txt-name").attr('value',this.getAttribute('goods_name'));
        obj.find("input.txt-price").attr('value',this.getAttribute('price'));
        trigger_img = $(this);

        layer.open({
            type: 1,
            content: obj[0].outerHTML,
            skin: 'layui-layer-rim',
            title: false,
            area:['650px','500px'],
        });
    });

    $(document).on('click', 'img.change_image', function () {
        show_img = $(this);
        $(this).closest('.layui-layer-content').find('input[type=file]').click();
    });

    $(document).on('change', 'input.change_image', function () {
        var file = this.files[0];
        if (file.size > 1024 * 1024 * 2) {
            layer.msg("图片不能大于2M");
            return;
        }
        if (file.type.indexOf('image') == -1) {
            layer.msg("只能上传图片");
            return;
        }

        var fr = new FileReader();
        fr.onload = function (e) {
            var index = layer.load();
            $.post("/admin/?act=web_config&op=upload_new_ad", { img: e.target.result }, function (msg) {
                layer.close(index);
                if (msg == 'failed') {
                    layer.msg("上传失败!");
                } else {
                    show_img.attr('src', msg);
                }
            });
        };

        fr.readAsDataURL(file);
    });

    $(document).on('click', '.save_img_link', function () {
        var img = $('.layui-layer-content img').attr('src');
        var link = $('.layui-layer-content input.txt-link').val();
        var goods_name = $('.layui-layer-content input.txt-name').val();
        var des = $('.layui-layer-content input.txt-des').val();
        var price = $('.layui-layer-content input.txt-price').val();
        trigger_img.attr({
            'src': img,
            link: link,
            'goods_name' : goods_name,
            'des' : des,
            'price' : price
        });
        //console.log(trigger_img);
        layer.closeAll();
    });


    $(document).on('change','#recommend_gcategory > select',function(){
            $(this).nextAll("select").remove();
            if (this.value > 0)
            {
                var _self = this;
                var url = SITEURL + '/index.php?act=index&op=josn_class&callback=?';
                $.getJSON(url, {'gc_id':this.value}, function(data){
                    if (data && data.length > 0)
                    {
                        $("<select class='class-select'><option>-请选择-</option></select>").insertAfter(_self);
                        var data  = data;
                        for (var i = 0; i < data.length; i++)
                        {
                            $(_self).next("select").append("<option data-explain='" + data[i].commis_rate + "' value='" + data[i].gc_id + "'>" + data[i].gc_name + "</option>");
                        }
                    }
                });
            }
    })

    $(document).on('click','.btn-search ',function(){
        var _body  = $(this).parents('.layui-layer-content');
        var gc_id = 0;
        $('#recommend_gcategory > select').each(function() {
            if ($(this).val()>0) gc_id = $(this).val();
        });
        var goods_name = $(this).siblings('input').val();
        if (gc_id>0 || goods_name!='') {
            $.ajax({
                type:  'GET',
                url:   'index.php?act=web_api&op=recommend_list&ajax=ajax',
                data:{
                    id:gc_id,
                    goods_name:goods_name
                },
                dataType: 'json',
                success: function(data){
                    var img_list = '';
                    $.each(data.goods_list,function(){
                        //console.log(data.goods_list);
                        img_list += '<li><div class="goods-pic" goods_name="'+this.goods_name+'" goods_img="'+this.goods_image+'" goods_id="'+this.goods_id+'" goods_des="'+this.goods_jingle+'" price="'+this.goods_price+'"><span class="thumb size-72x72"><img src="'+this.goods_image+'" height="72" width="72" /></span></div><div class="goods-name">"'+this.goods_name+'"</div></li>';
                    })
                    _body.find('#show_recommend_goods_list .dialog-goodslist-s2').html(img_list);
                    _body.find("#show_recommend_goods").html(data.page);
                    _body.find("#show_recommend_goods a").each(function(){
                        var href_ = $(this).attr('href');
                        $(this).attr('href_',href_);
                        $(this).removeAttr('href');
                    })
                }
            })

        }
    })

    $(document).on('click', '.save_json', function () {
        save_json();
    });

//# sourceMappingURL=model_edit.js.map


});

